import requests
import re
import os
import shutil

if os.path.exists('static/高清壁纸'):
    shutil.rmtree('static/高清壁纸')
    os.mkdir('static/高清壁纸')
else:
    os.mkdir('static/高清壁纸')

url = 'https://www.hdbz.net/'
response = requests.get(url)


response_text = response.content.decode('utf-8')

pattern = r'<a[^>]*href="([^"]*)"[^>]*title="([^"]*)"[^>]*>((?!</a>).)*?<img[^>]*data-original="([^"]*)"'
match = re.findall(pattern, response_text,re.S)
l = []
# print(len(match))   # 48
for item in match:
    path = item[0]
    title = item[1]
    img_src = item[3]
    l.append(img_src)
    process_response = requests.get(img_src)

    with open(f'static/高清壁纸/{title}_process.jpg','wb')as f:
        f.write(process_response.content)

    detail_url = "https://www.hdbz.net" + path

    detail_response = requests.get(detail_url).content.decode('utf-8')
    pattern = r'<img[^>]*?\balt="([^"]*)"[^>]*?\bsrc="([^"]*)"[^>]*?>'
    matches = re.findall(pattern, detail_response, re.DOTALL)

    for item in matches:
        title = item[0]
        img_src = item[1]
        print(title,img_src)
        with open(f'static/高清壁纸/{title}_show.jpg','wb')as f:
            img_response = requests.get(img_src)
            f.write(img_response.content)

print(len(l))
